from rest_framework import serializers

from goods.models import GoodsChannel, GoodsChannelGroup, GoodsCategory


class PindaoSerializer(serializers.ModelSerializer):
    category = serializers.StringRelatedField(label='一级分类')
    category_id = serializers.IntegerField(label='一级分类ID')
    group = serializers.StringRelatedField(label='频道组名称')
    group_id = serializers.IntegerField(label='频道组ID')

    class Meta:
        model = GoodsChannel
        fields = ('id', 'category', 'category_id', 'group', 'group_id', 'url', 'sequence')
        extra_kwargs = {
            'id': {'read_only': True},
            'category': {'read_only': True},
            'group': {'read_only': True}
        }

    def create(self, validated_data):
        data = GoodsChannel.objects.create(**validated_data)
        return data


class GoodsChanneltypesSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsChannelGroup
        fields = ('id', 'name')


class GoodsCategoriesSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ('id', 'name')
